package com.das.mechanic_base.widget;

import android.content.Context;
import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.das.mechanic_base.R;
import com.das.mechanic_base.adapter.main.X3CarChoiceAdapter;
import com.das.mechanic_base.adapter.main.X3EditAppearanceResultAdapter;
import com.das.mechanic_base.base.X3BaseBottomSheetDialog;
import com.das.mechanic_base.base.X3IBaseView;
import com.das.mechanic_base.bean.alone.AloneCarBean;
import com.das.mechanic_base.bean.alone.AloneImageAndRecordBean;
import com.das.mechanic_base.bean.main.CarTyreAloneBean;
import com.das.mechanic_base.mapi.api.NetWorkHttp;
import com.das.mechanic_base.mapi.response.HttpCallBack;
import com.das.mechanic_base.mapi.schedulers.RxSchedulersHelper;
import com.das.mechanic_base.utils.X3ScreenUtils;
import com.das.mechanic_base.utils.X3StringUtils;
import com.das.mechanic_base.utils.X3Utils;
import com.umeng.analytics.MobclickAgent;

import org.greenrobot.eventbus.EventBus;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;




public class X3EditAppearanceamageDialog extends X3BaseBottomSheetDialog implements X3CarChoiceAdapter.IOnClickChoice, View.OnClickListener {

    /**
     * 根据sn 赋值拍照题目
     */
    private HashMap<String, AloneCarBean> cameraAllList;

    /**
     * 根据sn 查找选择题 用于全部通过功能
     */
    private HashMap<String, X3EditAppearanceResultAdapter> choiceMap;
    /**
     * 存储答案
     */
    private HashMap<String,String> answerMap;

    private List<CarTyreAloneBean> mList;
    private long receiveBaseId;
    private LinearLayout ll_content;
    private Button btn_save;
    private TextView tv_cancle;

    public X3EditAppearanceamageDialog(@NonNull Context context) {
        super(context);
    }

    @Override
    protected boolean getDialogCancel() {
        return false;
    }

    @Override
    protected void initView() {
        ll_content = findViewById(R.id.ll_content);
        btn_save = findViewById(R.id.btn_save);
        btn_save.setOnClickListener(this);
        tv_cancle = findViewById(R.id.tv_cancel);
        tv_cancle.setOnClickListener(this);
    }

    @Override
    protected int getLayoutId() {
        return R.layout.x3_dialog_editappearancedamge;
    }

    /**
     * 请求轮胎轮毂数据
     * @param questionToSystemSn
     */
    public void setQuestionSystemSn(String questionToSystemSn,long receiveBaseId) {
        this.receiveBaseId = receiveBaseId;
        NetWorkHttp.getApi().getQuestionSystemSn(questionToSystemSn, receiveBaseId)
                .compose(((X3IBaseView)mContext).bindToLife())
                .compose(RxSchedulersHelper.defaultComposeRequest())
                .subscribe(new HttpCallBack<List<CarTyreAloneBean>>() {
                    @Override
                    protected void onDone(List<CarTyreAloneBean> carTyreAloneBeans) {
                        if (X3StringUtils.isListEmpty(carTyreAloneBeans)) {
                            return;
                        }
                        showTyreView(carTyreAloneBeans);
                    }

                    @Override
                    protected String LoadingMessage() {
                        return null;
                    }

                    @Override
                    protected void showError(String error) {

                    }
                });
    }
    private void showTyreView(List<CarTyreAloneBean> mList) {
        this.mList = mList;
        choiceMap = new HashMap<>();
        cameraAllList = new HashMap<>();
        answerMap = new HashMap<>();
        for (int i = 0; i < mList.size(); i++) {
            CarTyreAloneBean tyreAloneBean = mList.get(i);
            List<CarTyreAloneBean.QuestionBaseEntityListBean> questionList = tyreAloneBean.questionBaseEntityList;
            if (X3StringUtils.isListEmpty(questionList)) {
                continue;
            }
            showQuestionItem(questionList,tyreAloneBean.toItem);
        }
    }

        /**
         * 显示题目
         * @param questionList
         */
        private void showQuestionItem(List<CarTyreAloneBean.QuestionBaseEntityListBean> questionList,String title) {
            for (int i = 0; i < questionList.size(); i++) {
                CarTyreAloneBean.QuestionBaseEntityListBean questionBean = questionList.get(i);
                if (questionBean == null) {
                    return;
                }
                String type = questionBean.type;
                String sn = questionBean.sn;
                String answer = questionBean.answer;

                answerMap.put(sn,answer);

                View itemView = null;
               if ("CHOICE".equals(type)) {
                    itemView = View.inflate(mContext,R.layout.x3_detection_choice_item,null);
                    LinearLayout.LayoutParams choiceLayout = new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,LinearLayout.LayoutParams.WRAP_CONTENT);
                    choiceLayout.leftMargin = X3ScreenUtils.dipToPx(16,mContext);
                    choiceLayout.rightMargin = X3ScreenUtils.dipToPx(16,mContext);
                    choiceLayout.bottomMargin = X3ScreenUtils.dipToPx(28,mContext);
                    itemView.setLayoutParams(choiceLayout);
                    TextView tv_title = itemView.findViewById(R.id.tv_title);
                    RecyclerView rlv_choice = itemView.findViewById(R.id.rlv_choice);

                    tv_title.setText(questionBean.description + "");
                    rlv_choice.setLayoutManager(new LinearLayoutManager(mContext));
                    List<String> optionsList = questionBean.optionsList;
                    X3EditAppearanceResultAdapter choiceAdapter = new X3EditAppearanceResultAdapter(optionsList,mContext,sn);
                    rlv_choice.setAdapter(choiceAdapter);
                    choiceAdapter.setiOnClickChoice(this);
                    choiceAdapter.changeSelectChoice(answer);
                    choiceMap.put(sn,choiceAdapter);
                    ll_content.addView(itemView);
                }
        }

    }

    @Override
    public void iOnClickChoiceContent(String content, String sn) {
        answerMap.put(sn,content);
        System.out.println("当前的状态"+content+"  sn "+  sn);
    }

    @Override
    public void onClick(View v) {
        int id = v.getId();
        if (id == R.id.tv_cancel) {
            dismiss();
        } else if (id == R.id.btn_save) {
            if (!X3Utils.isFastClick()) {
                return;
            }
            if (X3StringUtils.isListEmpty(mList)) {
                return;
            }
            for (int i = 0; i < mList.size(); i++) {
                CarTyreAloneBean tyreAloneBean = mList.get(i);
                List<CarTyreAloneBean.QuestionBaseEntityListBean> questionList = tyreAloneBean.questionBaseEntityList;
                List<CarTyreAloneBean.QuestionBaseEntityListBean> newQuestionList = new ArrayList<>();

                String tyreSn = tyreAloneBean.sn;
                AloneCarBean carBean = cameraAllList.get(tyreSn);
                List<Long> idList = new ArrayList<>();
                if (carBean != null) {
                    List<AloneImageAndRecordBean> cameraList = carBean.getDetectionNewAdapter().getCameraList();
                    if (!X3StringUtils.isListEmpty(cameraList)) {
                        for (AloneImageAndRecordBean imageBean : cameraList) {
                            if (imageBean.getId() == 0) {
                                continue;
                            }
                            idList.add(imageBean.getId());
                        }
                    }
                }

                tyreAloneBean.resourceIdList = idList;

                if (X3StringUtils.isListEmpty(questionList)) {
                    mList.remove(i);
                    mList.add(i, tyreAloneBean);
                    continue;
                }
                for (int j = 0; j < questionList.size(); j++) {
                    CarTyreAloneBean.QuestionBaseEntityListBean questionBean = questionList.get(j);
                    String sn = questionBean.sn;
                    String content = answerMap.get(sn);
                    if (X3StringUtils.isEmpty(content)) {
                        questionBean.answer = "";
                    } else {
                        String afterAnswer = content.replace("A. ", "").replace("B. ", "").replace("C. ", "").replace("D. ", "").replace("E. ", "");
                        questionBean.answer = afterAnswer;
                    }
                    newQuestionList.add(questionBean);
                }
                tyreAloneBean.questionBaseEntityList = newQuestionList;
                mList.remove(i);
                mList.add(i, tyreAloneBean);
            }
            /**
             * 保存或者更新初检轮胎信息
             */
            saveDetection(mList);
        }
    }

    /**
     * 保存或者更新初检轮胎信息
     * @param mList
     */
    private void saveDetection(List<CarTyreAloneBean> mList) {
        NetWorkHttp.getApi().saveDetectionFirstInfoNew(mList,receiveBaseId)
                .compose(((X3IBaseView)mContext).bindToLife())
                .compose(RxSchedulersHelper.defaultComposeRequest())
                .subscribe(new HttpCallBack<Object>() {
                    @Override
                    protected void onDone(Object o) {
                        dismiss();
                        if (iOnRefresh != null) {
                            iOnRefresh.iOnRefreshData();
                        }
                        //通知初检信息页面刷新接口
                        EventBus.getDefault().post("UPDATE_PRELIMINARY_INSPECTION_INFORMATION");
                    }

                    @Override
                    protected String LoadingMessage() {
                        return null;
                    }

                    @Override
                    protected void showError(String error) {

                    }
                });

    }
   IOnRefresh iOnRefresh;

    public void setiOnRefresh(IOnRefresh iOnRefresh) {
        this.iOnRefresh = iOnRefresh;
    }

    /**
     * 刷新数据
     */
    public interface IOnRefresh {
        void iOnRefreshData();
    }
    @Override
    protected void onStart() {
        super.onStart();
        MobclickAgent.onPageStart("初检信息外观损伤编辑弹窗");
    }

    @Override
    protected void onStop() {
        super.onStop();
        MobclickAgent.onPageEnd("初检信息外观损伤编辑弹窗");
    }
}
